/*----------------------------------------------------------------------------
* This unit test tests the village card effect. 
*----------------------------------------------------------------------------*/
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 0

void checkVillage(struct gameState *state, int handPos) {
	int check,  choice1, choice2, choice3;
	struct gameState preG;	
	memcpy(&preG, state, sizeof(struct gameState));	
	
	int p = preG.whoseTurn;

	choice1 = floor(Random() * 256);
	choice2 = floor(Random() * 256);
	choice3 = floor(Random() * 256);
       
	check = cardEffect(village, choice1, choice2, choice3, state, handPos, 0);  

	drawCard(p, &preG); //draw a card
	preG.numActions = preG.numActions + 2; //add 2 actions
	discardCard(handPos, p, &preG, 0); //discard card


	// Check that result is correct
	if(check != 0)
		printf("Village card effect: FAIL check does not return 0. \n");	
	if(memcmp(&preG, state, sizeof(struct gameState)) != 0)
		printf("Village card effect: FAIL states are not the same. \n");	
	
}

int main() {
  	struct gameState G;
	int i, n, p, deckCt, discardCt, handCt, handPos, numPlayers, numTests = 50;
  	SelectStream(0);
  	PutSeed((long)2);  	

	printf ("TESTING village card effect:\n");

	//randomize number of players
	numPlayers = 2 + (int)floor(Random() * (MAX_PLAYERS - 2)); 
	
	for(n = 0; n < numTests; n++) {
		for (p = 0; p < numPlayers; p++) { 
			
			// Initialize the gameState with random bytes
			for (i = 0; i < sizeof(struct gameState); i++) {
				((char*)&G)[i] = floor(Random() * 255);
			}

			G.playedCardCount = floor(Random() * MAX_DECK);
			G.numBuys = 1;
			G.coins = 0;
			
			SelectStream(2);
			handCt = (int)floor(Random() * MAX_HAND);
			deckCt = (int)floor(Random() * MAX_DECK);
			discardCt = (int)floor(Random() * MAX_DECK);
			
			G.handCount[p] = handCt;
			G.deckCount[p] = deckCt;
			G.discardCount[p] = discardCt;
			
			memset(G.hand[p], 0, sizeof(int) * handCt);		
			memset(G.deck[p], 0, sizeof(int) * deckCt);
			memset(G.discard[p], 0, sizeof(int) * discardCt);

			// Randomize cards in deck and hand
			SelectStream(3);
			for (i=0; i < deckCt; i++)
				G.deck[p][i] = floor(Random() * (treasure_map+1));

			SelectStream(1);
			for (i=0; i < handCt; i++)
				G.hand[p][i] = floor(Random() * (treasure_map+1));						

			// Set current player and a handPos within range of handCount
			G.whoseTurn = p;
			G.numActions = 1;
			handPos = floor(Random() * (G.handCount[p]));	

			checkVillage(&G, handPos);
							
		}
	
	}
	printf("village card effect: PASS. \n");
	
	return 0;
}